<?php
/**
 * Each engineer has a duty to keep the code elegant
 * Created by wolffy.
 * Author @wolffy<13750088090@sina.cn>
 * Date: 2019/3/25
 * Time: 11:30
 */
namespace App\Services\QuickPay;

class QuickPayLService
{
    protected $apiUrl;
    protected $mchId;
    protected $appId;
    protected $apiKey;
    protected $tradeNotifyUrl;

    public function __construct()
    {
        $this->apiUrl = config('pay.fourpay.api_url');
        $this->mchId = config('pay.fourpay.mch_id');
        $this->appId = config('pay.fourpay.app_id');
        $this->apiKey = config('pay.fourpay.api_key');
        $this->tradeNotifyUrl = config('pay.fourpay.quick_pay_l.trade_notify_url');
    }

    /**
     * 获取接口请求结果
     * @param $param
     * @param $url
     * @return mixed
     */
    protected function getRequestResult($param, $url)
    {
        $param['sign'] = getSign($param, $this->apiKey);
        $client = new \GuzzleHttp\Client(['verify' => false]);
        $response = $client->request('post', $url, ['form_params' => $param]);
        $json = $response->getBody()->getContents();
        $result = json_decode($json, true);
        return [
            'params' => $param,
            'response' => $result
        ];
    }

    /**
     * 注册子商户
     * @param $tradeNo
     * @param $realname
     * @param $idCard
     * @param $bankCode
     * @param $bankName
     * @param $bankCardNo
     * @param $bankCardMobile
     * @return mixed
     */
    public function subMerchantRegister($tradeNo, $realname, $idCard, $bankCode, $bankName, $bankCardNo, $bankCardMobile)
    {
        $param['mch_id'] = $this->mchId;
        $param['app_id'] = $this->appId;
        $param['mch_no'] = $tradeNo;
        $param['realname'] = $realname;
        $param['id_card'] = $idCard;
        $param['bank_code'] = $bankCode;
        $param['bank_name'] = $bankName;
        $param['bank_card_no'] = $bankCardNo;
        $param['bank_card_mobile'] = $bankCardMobile;
        $param['sub_mch_rate'] = '0.006';
        $param['single_price'] = '200';
        $url = $this->apiUrl . 'api/pay/quick_pay_l/sub_merchant_register';
        return $this->getRequestResult($param, $url);
    }

    /**
     * 修改卡号和费率
     * @param $tradeNo
     * @param $subMchNo
     * @param $idCard
     * @param $bankCode
     * @param $bankCardNo
     * @param $bankCardMobile
     * @return mixed
     */
    public function subMerchantFee($tradeNo, $subMchNo, $realname, $bankCode, $bankName, $bankCardNo, $bankCardMobile, $rate, $singlePrice)
    {
        $param['mch_id'] = $this->mchId;
        $param['app_id'] = $this->appId;
        $param['mch_no'] = $tradeNo;
        $param['sub_mch_no'] = $subMchNo;
        $param['realname'] = $realname;
        $param['bank_code'] = $bankCode;
        $param['bank_name'] = $bankName;
        $param['bank_card_no'] = $bankCardNo;
        $param['bank_card_mobile'] = $bankCardMobile;
        $param['sub_mch_rate'] = $rate;
        $param['single_price'] = (string)round($singlePrice * 100, 2);
        $url = $this->apiUrl . 'api/pay/quick_pay_l/sub_merchant_fee';
        return $this->getRequestResult($param, $url);
    }

    /**
     * 交易
     * @param $tradeNo
     * @param $subMchNo
     * @param $bankCode
     * @param $bankCardNo
     * @param $bankCardMobile
     * @param $expireDate
     * @param $cvn2
     * @param $amount
     * @param $rate
     * @param $singlePrice
     * @param $address
     * @return mixed
     */
    public function trade($tradeNo, $subMchNo, $realname, $bankCode, $bankCardNo, $bankCardMobile, $expireDate, $cvn2,
                          $amount, $rate, $singlePrice)
    {
        $param['mch_id'] = $this->mchId;
        $param['app_id'] = $this->appId;
        $param['mch_no'] = $tradeNo;
        $param['sub_mch_no'] = $subMchNo;
        $param['realname'] = $realname;
        $param['bank_code'] = $bankCode;
        $param['bank_card_no'] = $bankCardNo;
        $param['bank_card_mobile'] = $bankCardMobile;
        $param['expire_date'] = $expireDate;
        $param['cvn2'] = $cvn2;
        $param['amount'] = (string)round($amount * 100, 2);
        $param['sub_mch_rate'] = $rate;
        $param['single_price'] = (string)round($singlePrice * 100, 2);
        $param['notify_url'] = $this->tradeNotifyUrl;
        $url = $this->apiUrl . 'api/pay/quick_pay_l/trade';
        return $this->getRequestResult($param, $url);
    }
}
